package com.robot.ad.controller;

import com.alibaba.fastjson.JSON;
import com.robot.ad.pojo.Banner;
import com.robot.ad.service.BannerService;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * banner控制器层
 *
 * @author wuyunbin
 */
@RestController
@CrossOrigin
@RequestMapping("/banner")
public class BannerJsonController {
    @Autowired
    private BannerService bannerService;

    /**
     * 轮播图列表-条件查询
     *
     * @param searchMap 查询条件封装
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @RequestMapping(value = "third/findBannerBySearch/{page}/{size}", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public String thirdFindBannerBySearch(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<Banner> pageList = bannerService.findSearch(searchMap, page, size);
        return JSON.toJSONString(new PageResult<>(pageList.getTotalElements(), pageList.getContent()));
    }

    /**
     * 根据ID查询轮播图详情
     *
     * @param id ID
     * @return
     */
    @RequestMapping(value = "third/findBannerById/{id}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public String thirdFindBannerById(@PathVariable String id) {
        return JSON.toJSONString(bannerService.findById(id));
    }

    /**
     * 增加轮播图
     *
     * @param ad
     */
    @RequestMapping(value = "third/addBannerByBanner", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public void thirdAddBannerByBanner(@RequestBody String ad) {
        bannerService.add(JSON.parseObject(ad, Banner.class));
    }

    /**
     * 修改轮播图
     *
     * @param ad
     */
    @RequestMapping(value = "third/updateBannerByBanner", method = RequestMethod.PUT, produces = "application/json;charset=utf-8")
    public void thirdUpdateBannerByBanner(@RequestBody String ad) {
        bannerService.update(JSON.parseObject(ad, Banner.class));
    }

    /**
     * 删除轮播图
     *
     * @param id
     */
    @RequestMapping(value = "third/deleteBannerById/{id}", method = RequestMethod.DELETE, produces = "application/json;charset=utf-8")
    public void thirdDeleteBannerById(@PathVariable String id) {
        bannerService.deleteById(id);
    }


    /**
     * 根据状态查找轮播图列表
     *
     * @param status
     * @return
     */
    @GetMapping(value = "third/findBannersByStatus/{status}", produces = "application/json;charset=utf-8")
    public String thirdFindBannersByStatus(@PathVariable String status) {
        return JSON.toJSONString(bannerService.findAllByStatusAndDeletedAtIsNullOrderBySortDesc(status));
    }
}
